from eventServer import eventServer
from gameServer import gameServer
from itemBase import ItemBase
from vec3 import Vec3
from soundServer import sound

class CollectCube(ItemBase):
    
    def __init__(self):
        ItemBase.__init__(self)
        
        self.setVar("base", "collectCube")
        self.setVar("sides",['t','t','t','t'])
        self.setVar("frictionImmune", True)
        
        self.setVar("timedUse", True)
        self.setVar("timedCollect", True)
        
        eventServer.register('itemCollected', self.onDrop)
        eventServer.register('goFast',self.onItemUse)
        
    def onDropped(self, player):
        pass
    
    def useTimeExpired(self, player):
        eventServer.pull('itemCollected',{'player':player, 'collected':None})
        sound.play("item_timeExpire")
    
    def onDrop(self, cmd, args):
        if(not(self.getVar("name") == args["collected"])):
            p = self.getVar("itemPlayer")
            if(not(p == None)):
                if(args['player'] == p.getVar('player')):
                    self.setVar("itemPlayer", None)
                    gameServer.removeObject(self.getVar("name"))
                    self.onDropped(p)
                
    def use(self, cmd, args):
        pass
    
    def onItemUse(self, cmd, args):
        p = self.getVar("itemPlayer")
        if(not(p == None)):
            if args['player'] == self.getVar("itemPlayer").getVar("player"):
                self.use(cmd, args)
            
    def update(self, cmd, args):
        ItemBase.update(self, cmd, args)
        
        player = self.getVar("itemPlayer")
        if(not(player == None)):
            p = self.getVar("pos")
            playP = player.getVar("pos")
            p.x = playP.x
            p.y = playP.y + 1
        
    def itemCollision(self, player):
        if(player.getVar("collectFlag")):
            self.setVar("itemPlayer", player)
            self.setVar("followTerrain", False)
            self.setVar("scale", Vec3(0.5, 0.5, 0.5))
            self.blink = False
            self.setVar("shown", True)
            self.setVar("collectTime", self.getVar("collectBlinkTime") + 1)
            eventServer.pull("itemCollected", {"player":player.getVar("player"), "collected":self.getVar("name")})
            self.onPickUp(player)
            sound.play("item_collect")
        
    def onPickUp(self, player):
        pass